#!/usr/bin/perl -w

sub parseFile{

my ($file) = @_;
open(IN, $file);
open(OUT, ">>hello");
%vertices;
$i = 1;
while ($line = <IN>){
    chomp($line);
    @arr = split(" ", $line);
    if (! exists $vertices{$arr[0]} ){
	$vertices{$arr[0]} = $i++;
    }
    $topoLine = $vertices{$arr[0]};
    while (defined($item = shift @arr)){
	print "I am here".$item;
	if ($item=~/^<([0-9]+)>$/){
	    if (! exists $vertices{$1}) {
		$vertices{$1} = $i++;
	    }
 	$topoLine = $topoLine." ".$vertices{$1};
	print "I am here\n";
	}
    }
    $topoLine = $topoLine."\n";
    print OUT $topoLine;
    $topoLine = "";
    @arr = ();
}

close(IN);
close(OUT);
}


sub copy{

    open(IN, "hello1");
    open(OUT, ">hello2");
    $vertices = $i-1;
    print OUT $vertices."\n";
    
    while (<IN>){
	chomp($_);
	print OUT $_."\n";
    } 
    close(IN);
    close(OUT);
}


foreach $num (0 .. $#ARGV) {
    parseFile($ARGV[$num]);
}
system('sort -k1n,1 hello>hello1');  
copy();
system('rm -rf hello');
system('rm -rf hello1');
